<template>
    <Form ref="formItem" :model="formItem" :label-width="120" :rules="ruleValidate">
		<FormItem label="类型" prop="type">
			<Select v-model="formItem.type" >
				<Option v-for="item in leavetype" :value="item.value" :key="item.value">{{item.label}}</Option>
			</Select>
		</FormItem>
		<FormItem label="几天不扣分" prop="num">
			<InputNumber v-model="formItem.num" :min="1" :step="1"></InputNumber>
		</FormItem>
		<FormItem label="超出每天扣多少分" prop="score">
			<Input v-model="formItem.score" :maxlength='5' placeholder="超出每天扣多少分"></Input>
		</FormItem>
		<FormItem>
		<Button type="primary" @click="submit('formItem')" :loading="btnload">{{infoId?'修改':'添加'}}</Button>
		</FormItem>
	</Form>
</template>

<script>
export default {
    props: {
        infoId:Number, 
    },
    data () {
        return {
            formItem: {                    
                type:null,
                num:null,
                score:'',
            },
            leavetype:[
                {
                    value:0,
                    label:'病假'
                },
                {
                    value:1,
                    label:'事假'
                },
            ],
            btnload:false,
            addUrl:'api/leave/v1/assessment_leave/edit',
            editUrl:'api/leave/v1/assessment_leave/getone/',
            updateUrl:'api/leave/v1/assessment_leave/update/',
            ruleValidate: {
                type: [
                    { required: true, type: 'number', message: '请选择类型', trigger: 'change' }
                ],
                num: [
                    { required: true, type: 'number', message: '请输入天数', trigger: 'blur' }
                ],
                score: [
                    { required: true, message: '请输入分数', trigger: 'blur' }
                ],
            }
        }
    },
    methods: {
        /************************************************************
         * 提交
         ************************************************************/
        submit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    this.btnload = true
                    if (this.infoId!=null) {
                        this.editSubmit ()
                    } else {
                        this.addSubmit ()
                    }
                } else {
                    this.$Message.error('验证失败');
                    this.btnload = false 
                }
            })
        },
        
        /************************************************************
         * 执行添加数据
         ************************************************************/
        addSubmit () {
            this.$http.post(this.addUrl,this.formItem).then(val => {
                this.$Message.success(val.message);
                this.$emit('requery',true);
                this.btnload = false
            }).catch(err => {
                if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                }
                this.btnload = false 
            })
        },
        
        /************************************************************
         * 执行修改数据
         ************************************************************/
        editSubmit () {
            this.$http.put(this.updateUrl+this.infoId,this.formItem).then(val=>{
                this.$Message.success(val.message);
                this.$emit('requery',true);
                this.btnload = false 
            }).catch(err => {
                if (err.code && err.code == 422) {
                    this.$Message.error(err.message);
                }
                this.btnload = false 
            })
        },
        
        /************************************************************
         * 单条数据查询（修改）
         ************************************************************/
        infodata(){
            this.$http.get(this.editUrl + this.infoId).then(val=>{ 
                let infoOld = JSON.parse(JSON.stringify(val.data))
                this.formItem.type = infoOld.type
				this.formItem.num = infoOld.num	
                // this.formItem.score = parseFloat(infoOld.score)   
                this.formItem.score = infoOld.score.toString()   
                        
            }).catch(err => {
                
            })
        },
    },
    created () {
        if (this.infoId!=null){            
            this.infodata ()
        } 
    },
}
</script>